package client.view;

import client.util.ClientUtil;
import common.model.entity.Request;
import common.model.entity.Response;
import common.model.entity.ResponseStatus;
import common.model.entity.User;
import server.DataBuffer;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * 注册界面
 */

public class RegisterFrame extends JFrame {
    private static final long serialVersionUID = -768631070458723803L;
    private JTextField nameField;
    private JPasswordField pwdField;

    public RegisterFrame(){
        this.init();
        setVisible(true);
    }

    public void init(){
        setTitle("OO账号注册");
        setIconImage(Toolkit.getDefaultToolkit().getImage("images\\logo.png"));
        //setUndecorated(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 480);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBounds(0, 0, 800, 447);
        contentPane.add(mainPanel);
        mainPanel.setLayout(null);

        JLabel TitleLabel = new JLabel("欢迎注册OO");
        TitleLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 43));
        TitleLabel.setBounds(320, 22, 249, 63);
        mainPanel.add(TitleLabel);

        JLabel subTitleLabel = new JLabel("每一天，乐在沟通");
        subTitleLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 30));
        subTitleLabel.setBounds(320, 83, 243, 53);
        mainPanel.add(subTitleLabel);

        nameField = new JTextField();
        nameField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 30));
        nameField.setBounds(320, 158, 441, 53);
        mainPanel.add(nameField);
        nameField.setColumns(10);

        pwdField = new JPasswordField();
        pwdField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 30));
        pwdField.setBounds(320, 221, 441, 53);
        mainPanel.add(pwdField);
        pwdField.setColumns(10);

        JButton registerButton = new JButton("");
        registerButton.setIcon(new ImageIcon("images\\regster.png"));
        registerButton.setBounds(320, 303, 437, 63);
        registerButton.setFocusPainted(false);
        mainPanel.add(registerButton);

        JCheckBox checkBox = new JCheckBox("我已阅读并同意相关服务条款和隐私政策 ");
        checkBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
        checkBox.setBackground(Color.WHITE);
        checkBox.setBounds(320, 380, 313, 35);
        checkBox.setFocusPainted(false);
        mainPanel.add(checkBox);

        JLabel vipLabel = new JLabel("申请靓号");
        vipLabel.setForeground(Color.RED);
        vipLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 25));
        vipLabel.setBounds(650, 88, 105, 46);
        mainPanel.add(vipLabel);

        JLabel rightLabel = new JLabel("");
        rightLabel.setIcon(new ImageIcon("images\\regright.jpg"));
        rightLabel.setBounds(0, 0, 298, 447);
        mainPanel.add(rightLabel);
        /*
        setBounds((DataBuffer.screenSize.width - 387)/2,
                (DataBuffer.screenSize.height - 267)/2,
                387, 267);
        ButtonGroup buttonGroup = new ButtonGroup();//单选按钮组
        buttonGroup.add(sex0);
        buttonGroup.add(sex1);

        head = new JComboBox();//下拉列表图标
        head.setBounds(278, 70, 65, 45);
        head.setMaximumRowCount(5);
        for (int i = 0; i < 11; i++) {
            head.addItem(new ImageIcon("images/" + i + ".png"));
            //通过循环添加图片，注意图片名字要取成1,2,3,4,5,等
        }
        head.setSelectedIndex(0);
        getContentPane().add(head);
         */
        //取消按钮监听事件处理
        /*
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent event) {
                RegisterFrame.this.dispose();
            }
        });*/
        //关闭窗口
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                RegisterFrame.this.dispose();
            }
        });
        /*
        // 重置事件处理
        nickname.setText("");
        pwdFld.setText("");
        pwd2Fld.setText("");
        nickname.requestFocusInWindow();//用户名获得焦点
        */


        vipLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null,"联系我获取靓号\nQQ：915669640");  //打开注册窗体
            }
        });
        //确认按钮监听事件处理
        registerButton.addActionListener(e -> {
            if (pwdField.getPassword().length==0) {
                JOptionPane.showMessageDialog(RegisterFrame.this, "带 “ * ” 为必填内容!");
                //判断用户名和密码是否为空
            } else if (!checkBox.isSelected()){
                JOptionPane.showMessageDialog(RegisterFrame.this, "请同意我们的协议!");
            } else {

                User user = new User(new String(pwdField.getPassword()),
                        nameField.getText());
                //sex0.isSelected() ? 'm' : 'f',
                //head.getSelectedIndex());
                try {
                    RegisterFrame.this.register(user);
                } catch (IOException | ClassNotFoundException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
    //注册方法
    private void register(User user) throws IOException, ClassNotFoundException{
        Request request = new Request();
        request.setAction("userRegiste");
        request.setAttribute("user", user);

        //获取响应
        Response response = ClientUtil.sendTextRequest(request);

        ResponseStatus status = response.getStatus();
        if (status == ResponseStatus.OK) {
            //int idx=1;
            //System.out.println(idx++);
            User user2 = (User) response.getData("user");
            //System.out.println(idx++);
            JOptionPane.showMessageDialog(RegisterFrame.this,
                    "恭喜您，您的Q号:" + user2.getId() + ",请牢记!!!",
                    "注册成功", JOptionPane.INFORMATION_MESSAGE);
            //System.out.println(idx++);
            this.setVisible(false);
        } else {
            JOptionPane.showMessageDialog(RegisterFrame.this,
                    "注册失败，请稍后再试！！！", "服务器内部错误！", JOptionPane.ERROR_MESSAGE);
        }
    }
}
